/*
 * Copyright 2008 Ayman Al-Sairafi ayman.alsairafi@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License
 *       at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jsyntaxpane.lexers;


import jsyntaxpane.Token;
import jsyntaxpane.TokenType;
 
%% 

%public
%class EsqlLexer
%extends DefaultJFlexLexer
%final
%unicode
%char
%type Token


%{
    /**
     * Create an empty lexer, yyrset will be called later to reset and assign
     * the reader
     */
    public EsqlLexer() {
        super();
    }

    @Override
    public int yychar() {
        return yychar;
    }

    private static final byte PARAN     = 1;
    private static final byte BRACKET   = 2;
    private static final byte CURLY     = 3;

%}

/* main character classes */
LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]

WhiteSpace = {LineTerminator} | [ \t\f]+

/* comments */
Comment = {TraditionalComment} | {EndOfLineComment} 

TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?

/* identifiers */
Identifier = [:jletter:][:jletterdigit:]*

/* integer literals */
DecIntegerLiteral = 0 | [1-9][0-9]*
DecLongLiteral    = {DecIntegerLiteral} [lL]

HexIntegerLiteral = 0 [xX] 0* {HexDigit} {1,8}
HexLongLiteral    = 0 [xX] 0* {HexDigit} {1,16} [lL]
HexDigit          = [0-9a-fA-F]

OctIntegerLiteral = 0+ [1-3]? {OctDigit} {1,15}
OctLongLiteral    = 0+ 1? {OctDigit} {1,21} [lL]
OctDigit          = [0-7]
    
/* floating point literals */        
FloatLiteral  = ({FLit1}|{FLit2}|{FLit3}) {Exponent}? [fF]
DoubleLiteral = ({FLit1}|{FLit2}|{FLit3}) {Exponent}?

FLit1    = [0-9]+ \. [0-9]* 
FLit2    = \. [0-9]+ 
FLit3    = [0-9]+ 
Exponent = [eE] [+-]? [0-9]+

/* string and character literals */
StringCharacter = [^\r\n\"\\]
SingleCharacter = [^\r\n\'\\]

%state STRING, CHARLITERAL, JDOC, JDOC_TAG

%%

<YYINITIAL> {

  /* keywords 1st and second layer*/
  "database"                     |
  "database.relations"           |
  "database.entries"                { return token(TokenType.COMMENT2); }           

 /* keywords operations*/
    "update"                     |
    "delete"                        { return token(TokenType.COMMENT); }       

  /* operation types*/
  "cascade"                      |
  "set null"                     |
  "restrict"                     |
  "no action"                       { return token(TokenType.STRING); }

/* attributes*/
  "+1"                       { return token(TokenType.DELIMITER); }

  /* Advanced methods */
  "length"                       |
  "type"                         { return token(TokenType.TYPE2); }

  /* Advanced types */
  "STRING"                { return token(TokenType.ERROR); } 
  "VARCHAR"               | 
  "DATE"                  |
  "INTEGER"               |
  "DOUBLE"                |
  "TIME"                  |
  "TINYINT"               |
  "TEXT"                  |
  "TINYTEXT"              |
  "MEDIUMTEXT"            |
  "LONGTEXT"              |
  "CHAR"                  |
  "BINARY"                | 
  "VARBINARY"             |
  "TINYBLOB"              |
  "MEDIUMBLOB"            |
  "BLOB"                  |
  "LONGBLOB"              |
  "SMALLINT"              |
  "MEDIUMINT"             |
  "BIGINT"                |
  "DECIMAL"               |
  "FLOAT"                 |
  "REAL"                  |
  "BIT"                   |
  "BOOLEAN"               |
  "SERIAL"                |
  "DATETIME"              |
  "TIMESTAMP"             |
  "YEAR"                  |
  "GEOMETRY"              |
  "POINT"                 |
  "LINESTRING"            |
  "POLYGON"               |
  "MULTIPOINT"            |
  "MULTILINESTRING"       |
  "MULTIPOLYGON"          |
  "GEOMETRYCOLLECTION"    { return token(TokenType.TYPE); }

  /* operators */

  "("                            { return token(TokenType.COMMENT); }
  ")"                            { return token(TokenType.COMMENT); }
  ","                            | 
  "->"                           | 
  "."                            { return token(TokenType.DELIMITER); } 
  
}


/* error fallback */
.|\n                             {  }
<<EOF>>                          { return null; }

